<?php
class StaffTypesController extends AppController {

	var $name = 'StaffTypes';
	var $components = array('Auth');
	var $helpers = array('Html');
	
	function beforeFilter() {		
		parent::beforeFilter();
		$this->layout="admin";
		$this->Auth->authorize = 'controller';		
	}

    function isAuthorized() {
		$MY_RULE_ID = 7; //Xem id tuong ung voi controller nay trong DB: table rules, vi du controller Staff/StaffTypes la 7.
        $allow = $this->requestAction('ruleStaffTypes/allow/'.$MY_RULE_ID);
		//Check xem co quyen su dung khong.
		if ($allow){
			return true;
		}else{
			$this->Session->setFlash(__('Permission denied!',true));
			$this->redirect(array("controller"=>"staffs","action"=>"login","admin"=>false));
			return false;
		}        
        return false;
    }
	function index() {		
		$this->redirect(array('action' => 'index','admin'=>true));
	}
	function admin_index() {
		$this->StaffType->recursive = 0;
		$this->set('staffTypes', $this->paginate());
	}

	function admin_view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid staff type', true));
			$this->redirect(array('action' => 'index'));
		}
		$this->set('staffType', $this->StaffType->read(null, $id));
	}

	function admin_add() {
		if (!empty($this->data)) {
			$this->StaffType->create();			
			if ($this->StaffType->save($this->data)) {
				$this->Session->setFlash(__('The staff type has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The staff type could not be saved. Please, try again.', true));
			}
		}
		$rules = $this->StaffType->Rule->find('list');
		$this->set(compact('rules'));		
	}

	function admin_edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid staff type', true));
			$this->redirect(array('action' => 'index'));
		}
		if (!empty($this->data)) {
			if ($this->StaffType->save($this->data)) {
				$this->Session->setFlash(__('The staff type has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The staff type could not be saved. Please, try again.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->StaffType->read(null, $id);
		}
		$rules = $this->StaffType->Rule->find('list');
		$this->set(compact('rules'));
	}

	function admin_delete($id = null) {
		$this->layout="ajax";
		if (!$id) {
			$this->set("delete_error",__('Invalid id for staff type', true));
			//$this->redirect(array('action'=>'index'));
		}
		$this->StaffType->recursive = 1;
		$count = $this->StaffType->Staff->find('count', array('conditions' => array('Staff.staff_type_id' => $id)));
		if ($count>0){
			$this->set("delete_error",__("Can't delete! This user type is asigned to ".$count." users!",true));
		}else{
			if ($this->StaffType->delete($id)) {
			}else $this->set("delete_error",__('Staff type was not deleted', true));			
			//$this->redirect(array('action' => 'index'));
		}		
	}
}
?>